﻿using Kimd.Logic;
using Kimd.Motion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkStation.Common;

namespace WorkStation.Logic
{
    public class CommonMethod
    {
        public static bool AllArmTesterNoProduct(int headerIndex)
        {
            List<Tray> testerTrays = new List<Tray>();
            testerTrays.Add((FlowManager.Instance.SelectFlow($"测试模组{headerIndex + 1}1") as TesterLogic).TesterTray);
            testerTrays.Add((FlowManager.Instance.SelectFlow($"测试模组{headerIndex + 1}2") as TesterLogic).TesterTray);
            testerTrays.Add((FlowManager.Instance.SelectFlow($"测试模组{headerIndex + 1}3") as TesterLogic).TesterTray);
            bool noProduct = true;
            for (int i = 0; i < testerTrays.Count; i++)
            {
                if (testerTrays[i].Helper.Any(c => c.Status == CellStatus.HasProduct))
                {
                    noProduct = false;
                    break;
                }
            }
            if (!noProduct)
                return noProduct;
            return noProduct;
        }
        public static bool AllTesterNoProduct(int moduleIndex)
        {
            int moduleIndexTemp = moduleIndex;
            List<Tray> testerTrays = new List<Tray>();
            testerTrays.Add((FlowManager.Instance.SelectFlow($"测试模组{moduleIndexTemp + 1}1") as TesterLogic).TesterTray);
            testerTrays.Add((FlowManager.Instance.SelectFlow($"测试模组{moduleIndexTemp + 1}2") as TesterLogic).TesterTray);
            testerTrays.Add((FlowManager.Instance.SelectFlow($"测试模组{moduleIndexTemp + 1}3") as TesterLogic).TesterTray);
            bool noProduct = true;
            for (int i = 0; i < testerTrays.Count; i++)
            {
                if (testerTrays[i].Helper.Any(c => c.Status == CellStatus.HasProduct))
                {
                    noProduct = false;
                    break;
                }
            }
            if (!noProduct)
                return noProduct;

            moduleIndexTemp++;
            testerTrays = new List<Tray>();
            testerTrays.Add((FlowManager.Instance.SelectFlow($"测试模组{moduleIndexTemp + 1}1") as TesterLogic).TesterTray);
            testerTrays.Add((FlowManager.Instance.SelectFlow($"测试模组{moduleIndexTemp + 1}2") as TesterLogic).TesterTray);
            testerTrays.Add((FlowManager.Instance.SelectFlow($"测试模组{moduleIndexTemp + 1}3") as TesterLogic).TesterTray);
            noProduct = true;
            for (int i = 0; i < testerTrays.Count; i++)
            {
                if (testerTrays[i].Helper.Any(c => c.Status == CellStatus.HasProduct))
                {
                    noProduct = false;
                    break;
                }
            }
            return noProduct;
        }
        public static bool AllTesterNoProductSingle(int headerIndex)
        {
            List<Tray> testerTrays = new List<Tray>();
            testerTrays.Add((FlowManager.Instance.SelectFlow($"测试模组{headerIndex + 1}1") as TesterLogic).TesterTray);
            testerTrays.Add((FlowManager.Instance.SelectFlow($"测试模组{headerIndex + 1}2") as TesterLogic).TesterTray);
            testerTrays.Add((FlowManager.Instance.SelectFlow($"测试模组{headerIndex + 1}3") as TesterLogic).TesterTray);
            bool noProduct = true;
            for (int i = 0; i < testerTrays.Count; i++)
            {
                if (testerTrays[i].Helper.Any(c => c.Status == CellStatus.HasProduct))
                {
                    noProduct = false;
                    break;
                }
            }
            if (!noProduct)
                return noProduct;
            return noProduct;
        }
        public static bool AllArmNoProduct(int moduleIndex)
        {
            if (moduleIndex == 0)
            {
                bool arm1HasProduct = (FlowManager.Instance.SelectFlow($"Arm1模组") as TransferLogic).GetFeedTray().Helper.Any(c => c.Status == CellStatus.HasProduct);
                bool arm2HasProduct = (FlowManager.Instance.SelectFlow($"Arm2模组") as TransferLogic).GetFeedTray().Helper.Any(c => c.Status == CellStatus.HasProduct);
                bool arm1HasProduct2 = (FlowManager.Instance.SelectFlow($"Arm1模组") as TransferLogic).GetRecycleTray().Helper.Any(c => c.Status == CellStatus.HasProduct);
                bool arm2HasProduct2 = (FlowManager.Instance.SelectFlow($"Arm2模组") as TransferLogic).GetRecycleTray().Helper.Any(c => c.Status == CellStatus.HasProduct);

                return !(arm1HasProduct || arm2HasProduct || arm1HasProduct2 || arm2HasProduct2);
            }
            else
            {
                bool arm3HasProduct = (FlowManager.Instance.SelectFlow($"Arm3模组") as TransferLogic).GetFeedTray().Helper.Any(c => c.Status == CellStatus.HasProduct);
                bool arm4HasProduct = (FlowManager.Instance.SelectFlow($"Arm4模组") as TransferLogic).GetFeedTray().Helper.Any(c => c.Status == CellStatus.HasProduct);
                bool arm3HasProduct2 = (FlowManager.Instance.SelectFlow($"Arm3模组") as TransferLogic).GetRecycleTray().Helper.Any(c => c.Status == CellStatus.HasProduct);
                bool arm4HasProduct2 = (FlowManager.Instance.SelectFlow($"Arm4模组") as TransferLogic).GetRecycleTray().Helper.Any(c => c.Status == CellStatus.HasProduct);

                return !(arm3HasProduct || arm4HasProduct || arm3HasProduct2 || arm4HasProduct2);
            }
        }
        public static bool AllArmNoRecycleProduct(int moduleIndex)
        {
            if (moduleIndex == 0)
            {
                bool arm1HasProduct2 = (FlowManager.Instance.SelectFlow($"Arm1模组") as TransferLogic).GetRecycleTray().Helper.Any(c => c.Status == CellStatus.HasProduct);
                bool arm2HasProduct2 = (FlowManager.Instance.SelectFlow($"Arm2模组") as TransferLogic).GetRecycleTray().Helper.Any(c => c.Status == CellStatus.HasProduct);

                return !(arm1HasProduct2 || arm2HasProduct2);
            }
            else
            {
                bool arm3HasProduct2 = (FlowManager.Instance.SelectFlow($"Arm3模组") as TransferLogic).GetRecycleTray().Helper.Any(c => c.Status == CellStatus.HasProduct);
                bool arm4HasProduct2 = (FlowManager.Instance.SelectFlow($"Arm4模组") as TransferLogic).GetRecycleTray().Helper.Any(c => c.Status == CellStatus.HasProduct);

                return !(arm3HasProduct2 || arm4HasProduct2);
            }
        }
        public static bool AllArmNoProductSingle(int headerIndex)
        {
            bool arm1HasProduct = (FlowManager.Instance.SelectFlow($"Arm{headerIndex + 1}模组") as TransferLogic).GetFeedTray().Helper.Any(c => c.Status == CellStatus.HasProduct);
            bool arm1HasProduct2 = (FlowManager.Instance.SelectFlow($"Arm{headerIndex + 1}模组") as TransferLogic).GetRecycleTray().Helper.Any(c => c.Status == CellStatus.HasProduct);

            return !(arm1HasProduct || arm1HasProduct2);
        }
        public static bool ArmRecycleHasProductSingle(int headerIndex)
        {
            bool arm1HasProduct2 = (FlowManager.Instance.SelectFlow($"Arm{headerIndex + 1}模组") as TransferLogic).GetRecycleTray().Helper.Any(c => c.Status == CellStatus.HasProduct);
            return arm1HasProduct2;
        }
        public static bool AllPrecisionNoProduct(int moduleIndex)
        {
            bool feed1UpHasProduct = (FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetFeedTray1Up().Helper.Any(c => c.Status == CellStatus.HasProduct);
            bool feed2UpHasProduct = (FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetFeedTray2Up().Helper.Any(c => c.Status == CellStatus.HasProduct);
            bool recyle1UpHasProduct = (FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetRecycleTray1Up().Helper.Any(c => c.Status == CellStatus.HasProduct);
            bool recyle2UpHasProduct = (FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetRecycleTray2Up().Helper.Any(c => c.Status == CellStatus.HasProduct);
            bool feed1DownHasProduct = (FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetFeedTray1Down().Helper.Any(c => c.Status == CellStatus.HasProduct);
            bool feed2DownHasProduct = (FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetFeedTray2Down().Helper.Any(c => c.Status == CellStatus.HasProduct);
            bool recyle1DownHasProduct = (FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetRecycleTray1Down().Helper.Any(c => c.Status == CellStatus.HasProduct);
            bool recyle2DownHasProduct = (FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetRecycleTray2Down().Helper.Any(c => c.Status == CellStatus.HasProduct);
            return !(feed1UpHasProduct ||
                     feed2UpHasProduct ||
                     recyle1UpHasProduct ||
                     recyle2UpHasProduct ||
                     feed1DownHasProduct ||
                     feed2DownHasProduct ||
                     recyle1DownHasProduct ||
                     recyle2DownHasProduct);
        }
        public static bool AllRobotNoProdcut(int moduleIndex)
        {
            Tray robotFeedTray = (FlowManager.Instance.SelectFlow($"Robot{moduleIndex + 1}模组") as RobotLogic).GetFeedTray();
            Tray robotRecycleTray = (FlowManager.Instance.SelectFlow($"Robot{moduleIndex + 1}模组") as RobotLogic).GetRecycleTray();
            bool robotFeedHasProduct = robotFeedTray.Helper.Any(c => c.Status == CellStatus.HasProduct);
            bool robotRecycleHasProduct = robotRecycleTray.Helper.Any(c => c.Status == CellStatus.HasProduct);
            return !(robotFeedHasProduct || robotRecycleHasProduct);
        }
        public static bool AllPrecisionFeedNoProduct(int moduleIndex)
        {
            return !((FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetFeedTray1Up().Helper.Any(c => c.Status == CellStatus.HasProduct) ||
                     (FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetFeedTray2Up().Helper.Any(c => c.Status == CellStatus.HasProduct) ||
                     (FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetFeedTray1Down().Helper.Any(c => c.Status == CellStatus.HasProduct) ||
                     (FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetFeedTray2Down().Helper.Any(c => c.Status == CellStatus.HasProduct));
        }
        public static bool AllPrecisionFeedNoProduct(int moduleIndex, bool isUp)
        {
            if (isUp)
            {
                return !((FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetFeedTray1Up().Helper.Any(c => c.Status == CellStatus.HasProduct) ||
                         (FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetFeedTray2Up().Helper.Any(c => c.Status == CellStatus.HasProduct));
            }
            else
            {
                return !((FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetFeedTray1Down().Helper.Any(c => c.Status == CellStatus.HasProduct) ||
                         (FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetFeedTray2Down().Helper.Any(c => c.Status == CellStatus.HasProduct));
            }
        }
        public static bool AllPrecisionRecycleNoProduct(int moduleIndex)
        {
            return !((FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetRecycleTray1Up().Helper.Any(c => c.Status == CellStatus.HasProduct) ||
                     (FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetRecycleTray2Up().Helper.Any(c => c.Status == CellStatus.HasProduct) ||
                     (FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetRecycleTray1Down().Helper.Any(c => c.Status == CellStatus.HasProduct) ||
                     (FlowManager.Instance.SelectFlow($"精定位移载{moduleIndex + 1}") as PrecisionLogic).GetRecycleTray2Down().Helper.Any(c => c.Status == CellStatus.HasProduct));
        }
        public static bool BufferHasProduct(int moduleIndex)
        {
            string moduleName = moduleIndex == 0 ? "前Arm模组" : "后Arm模组";
            return ((FlowManager.Instance.SelectFlow(moduleName) as PutOKNGLogic).GetBufferTray1().Helper.Any(c => c.Status == CellStatus.HasProduct) ||
                    (FlowManager.Instance.SelectFlow(moduleName) as PutOKNGLogic).GetBufferTray2().Helper.Any(c => c.Status == CellStatus.HasProduct));
        }
        public static bool ANGHasProduct(int moduleIndex)
        {
            if(moduleIndex == 0)
            {
                Tray angtray = (FlowManager.Instance.SelectFlow("前Arm模组") as PutOKNGLogic).GetANGTray();
                return angtray.Helper.Any(c => c.Status == CellStatus.HasProduct);
            }
            else
            {
                Tray angtray = (FlowManager.Instance.SelectFlow("后Arm模组") as PutOKNGLogic).GetANGTray();
                return angtray.Helper.Any(c => c.Status == CellStatus.HasProduct);
            }
        }
        public static bool ABNGHasProduct(int moduleIndex)
        {
            if (moduleIndex == 0)
            {
                Tray abngtray1 = (FlowManager.Instance.SelectFlow("前Arm模组") as PutOKNGLogic).GetABNGTray();
                return abngtray1.Helper.Any(c => c.Status == CellStatus.HasProduct);
            }
            else
            {
                Tray abngtray1 = (FlowManager.Instance.SelectFlow("后Arm模组") as PutOKNGLogic).GetABNGTray();
                return abngtray1.Helper.Any(c => c.Status == CellStatus.HasProduct);
            }
        }
        public static bool DoFeedingCheck(string sn, out string errInfo)
        {
            errInfo = "";
            try
            {
                string res = Mes.FeedingCheck(sn);
                FeedingCheck_Result feedingCheck_Results = (FeedingCheck_Result)Newtonsoft.Json.JsonConvert.DeserializeObject(res, typeof(FeedingCheck_Result));
                if (!feedingCheck_Results.RESULT.Contains("FAIL"))
                {
                    return true;
                }
                errInfo = feedingCheck_Results.MSG;
                return false;
            }
            catch(Exception ex)
            {
                errInfo = ex.Message;
                return false;
            }
        }
        public static bool DoMesCheckIn(string sn, out string msg)
        {
            msg = "";
            try
            {
                string res = Mes.CheckIN(sn, SystemConfig.Instance.MesParam.LotNo);
                CheckIN_Result checkIN_Results = (CheckIN_Result)Newtonsoft.Json.JsonConvert.DeserializeObject(res, typeof(CheckIN_Result));
                if (!checkIN_Results.RESULT.Contains("FAIL"))
                {
                    msg = checkIN_Results.MSG;
                    return true;
                }
                msg = checkIN_Results.MSG;
                return false;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                return false;
            }
        }
        public static bool DoCarrierBind(string sn, string cavityNo, string carrierID, out string errInfo)
        {
            errInfo = "";
            try
            {
                string res = Mes.CarrierBind(sn, cavityNo, carrierID);
                CarrierBind_Result carrierBind_Results = (CarrierBind_Result)Newtonsoft.Json.JsonConvert.DeserializeObject(res, typeof(CarrierBind_Result));
                if (!carrierBind_Results.RESULT.Contains("FAIL"))
                {
                    return true;
                }
                errInfo = carrierBind_Results.MSG;
                return false;
            }
            catch (Exception ex)
            {
                errInfo = ex.Message;
                return false;
            }
        }
        public static bool DoCarrierCheck(string carrierID, out string errInfo)
        {
            errInfo = "";
            try
            {
                string res = Mes.CarrierCheck(carrierID);
                CarrierCheck_Result carrierCheck_Result = (CarrierCheck_Result)Newtonsoft.Json.JsonConvert.DeserializeObject(res, typeof(CarrierCheck_Result));
                if (!carrierCheck_Result.RESULT.Contains("FAIL"))
                {
                    return true;
                }
                errInfo = carrierCheck_Result.MSG;
                return false;
            }
            catch (Exception ex)
            {
                errInfo = ex.Message;
                return false;
            }
        }
        public static string ConvertShortArrayToString(short[] vals)
        {
            byte[] barcodeByte = new byte[vals.Length * 2];
            for (int i = 0; i < vals.Length; i++)
            {
                barcodeByte[2 * i] = (byte)(vals[i] % 256);
                barcodeByte[2 * i + 1] = (byte)(vals[i] / 256);
            }
            return ASCIIEncoding.ASCII.GetString(barcodeByte).Replace("\0", "");
        }
        public static void SetOnoff(ref short status, int statusBitNo, bool onOff)
        {
            int data = 0;
            data = status;
            if (onOff)
                data |= 0x01 << statusBitNo;
            else
                data &= ~(0x01 << statusBitNo);
            status = (short)data;
        }
    }
}
